package com.lowkey.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.lowkey.tableInfo.Id;
import com.lowkey.tableInfo.ManyToOne;
import com.lowkey.tableInfo.OneToMany;
import com.lowkey.tableInfo.Property;
import com.lowkey.tableInfo.TableInfo;
@SuppressWarnings("unchecked")
///所有bean下的实体配置文件,将其对象化为个个tableInfo,并保存在一个Map<String,TableInfo>中！
public class TableInfoXMLUitls {
	public static Map<String,TableInfo> getTableInfoXML(List<String> filelist){
		Map<String,TableInfo> tableMap = new HashMap<String,TableInfo>();
 
		for(String fileName:filelist){
			TableInfo tableInfo = new TableInfo();
			File file = new File(fileName);
			SAXReader reader=new SAXReader();
			Document doc=null;
			try {
				doc=reader.read(file);
				Element root = doc.getRootElement();
				tableInfo.setClassName(root.attribute("name").getText()); 
				tableInfo.setTableName(root.attribute("table").getText()); 
				Element id = root.element("Id"); 
				Id tableInfoId = new Id();
				tableInfoId.setColumn(id.attribute("column").getText());
				tableInfoId.setName(id.attribute("name").getText());
				Attribute id_UpBySelf = id.attribute("upBySelf");
				if(id_UpBySelf!=null){
					tableInfoId.setUpByself(Boolean.parseBoolean(id_UpBySelf.getValue()));
				}
				
				tableInfo.setId(tableInfoId);
				List<Element> elist=root.elements();
				List<Property> propertys = null;
				List<OneToMany> oneList = null;
				List<ManyToOne> manyList = null;
				for(Element e : elist ){
					if(e.getName().equals("property")){
						if(null==propertys){
							propertys = new ArrayList<Property>();
						}
						Property p = new Property();
						p.setName(e.attribute("name").getText());
						p.setColumn(e.attribute("column").getText());
						Attribute df = e.attribute("default");
						if(df!=null){
							p.setDefaultValue(df.getValue());
						}
						propertys.add(p);
					}else if (e.getName().equals("onetomany")){
						if(null==oneList){
							oneList = new ArrayList<OneToMany>();
						}
						OneToMany o = new OneToMany();
						o.setOneName(e.attributeValue("name"));
						o.setOneClass(e.attributeValue("class"));
						o.setOneColumn(e.element("column").attribute("name").getText());
						oneList.add(o);
					}else if (e.getName().equals("manytoone")){
						if(null==manyList){
							manyList = new ArrayList<ManyToOne>();
						}
						ManyToOne m = new ManyToOne();
						m.setManyName(e.attributeValue("name"));
						m.setManyClass(e.attributeValue("class"));
						m.setManyColumn(e.element("column").attribute("name").getText());
						manyList.add(m);
					}
				}
				tableInfo.setPropertys(propertys);
				tableInfo.setOneList(oneList);
				tableInfo.setManyList(manyList);
				tableMap.put(tableInfo.getClassName(), tableInfo);
			} catch (DocumentException e) {
				e.printStackTrace();
			}
		}
		return tableMap;
	}
	
 
}
